﻿using System.Collections.Generic;
using System.Linq;
using System.Text;

using Protocol.SouthGridProtocol.Blocks;
using Protocol.SouthGridProtocol.Decoder;
using Protocol.SouthGridProtocol.Encoder;
using PacoToolKit.Format;
namespace Protocol.SouthGridProtocol.Model
{
    public class C0DH
    {
        private static string afn = "0D";
        private static string head = "68";
        private static string bottom = "16";

        private static Seq seq = new Seq(0, 1, 1, 1, 4);
        private static C c = new C(0, 1, 1, null, 0, 11);//prm=1,funcode=11

        /// <summary>
        /// 组合查询历史数据
        /// </summary>
        /// <param name="concentrator"></param>
        /// <param name="dataList"></param>
        /// <returns></returns>
        public static string encodeC0DH(string concentrator, List<CDataWithTimeDownLine> dataList)
        {
            //主站启动的发送帧的MSA应为非零值,终端启动发送帧的MSA应为零，其主站响应帧的MSA也应为零
            concentrator = concentrator.Substring(0, 12) + "01";

            StringBuilder sb = new StringBuilder();
            foreach (CDataWithTimeDownLine data in dataList)
            {
                sb.Append(data.da); sb.Append(data.di); sb.Append(data.beginTime); sb.Append(data.endTime); sb.Append(data.m);
            }
            StringBuilder msgsb = new StringBuilder();
            string length = BaseEncoder.CalcLength(c.encodeC() + concentrator + afn + seq.encodeSeqString() + sb.ToString());
            string cs = BaseEncoder.CalcCSCode(c.encodeC() + concentrator + afn + seq.encodeSeqString() + sb.ToString());
            return head + length + length + head + c.encodeC() + concentrator + afn + seq.encodeSeqString() + sb.ToString() + cs + bottom;
        }

        /// <summary>
        /// 解析冻结
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static List<C0DData> decodeC0DH(string msg)
        {
            List<C0DData> dataList = new List<C0DData>();
            //int msgLength = BaseDecoder.GetLength(msg);//字节数 
            string dataRemain = msg.Remove(0, 32);
            while (dataRemain.Length > 4)
            {
                string da = dataRemain.Substring(0, 4);
                string di = dataRemain.Substring(4, 8);
                int currentDataBlockLength;
                if (DI0506XXFF.Contains(Formater.ReverseHexString(di)) || DI00XXFF01.Contains(Formater.ReverseHexString(di)) || DI00XXFF00.Contains(Formater.ReverseHexString(di)))
                {
                    //多组费率
                }
                else
                {
                    currentDataBlockLength = getDataLength(Formater.ReverseHexString(di));
                    //add 2014-4-16
                    //查询本di长度，为0则不属于日记录，月记录，为数据密度＜5的di，在当前数据中匹配长度
                    if (currentDataBlockLength == 0)
                    {
                        currentDataBlockLength = C0CH.GetDataLength(Formater.ReverseHexString(di));
                    }
                    dataList.Add(new C0DData
                    {
                        da = da,
                        di = di,
                        data = dataRemain.Substring(12, currentDataBlockLength * 2),
                        time = dataRemain.Substring(12 + currentDataBlockLength * 2, 12)
                    });
                    dataRemain = dataRemain.Remove(0, 4 + 8 + currentDataBlockLength * 2 + 12);
                }
            }
            return dataList;
        }

        /// <summary>
        /// 解析小时冻结数据块
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="TimeRecordCount"></param>
        /// <returns></returns>
        public static List<C0DData> decodeC0DH(string msg, int TimeRecordCount)
        {
            List<C0DData> dataList = new List<C0DData>();
            string dataRemain = msg.Remove(0, 32);
            while (dataRemain.Length > 4)
            {
                string da = dataRemain.Substring(0, 4);
                string di = dataRemain.Substring(4, 8);
                int currentDataBlockLength;
                string currentDataBlock;

                #region 0001FF00
                if (Formater.ReverseHexString(di).Equals("0001FF00"))
                {
                    //费率数
                    int rateNum = int.Parse(Formater.HEXtoDEC(dataRemain.Substring(12, 2)));

                    currentDataBlockLength = (1 + 4 * (rateNum+1) + 6) * TimeRecordCount;
                    currentDataBlock = dataRemain.Substring(12, currentDataBlockLength * 2);

                    if (rateNum == 4)
                    {
                        for (int i = 0; i < TimeRecordCount; i++)
                        {
                            string data1 = currentDataBlock.Substring(i * currentDataBlockLength * 2 / TimeRecordCount + 2, 8);
                            string data2 = currentDataBlock.Substring(i * currentDataBlockLength * 2 / TimeRecordCount + 2 + 8, 8);
                            string data3 = currentDataBlock.Substring(i * currentDataBlockLength * 2 / TimeRecordCount + 2 + 8 + 8, 8);
                            string data4 = currentDataBlock.Substring(i * currentDataBlockLength * 2 / TimeRecordCount + 2 + 8 + 8 + 8, 8);
                            string data5 = currentDataBlock.Substring(i * currentDataBlockLength * 2 / TimeRecordCount + 2 + 8 + 8 + 8 + 8, 8);
                            string time = currentDataBlock.Substring(i * currentDataBlockLength * 2 / TimeRecordCount + 2 + 8 + 8 + 8 + 8 + 8, 12);

                            dataList.Add(new C0DData()
                            {
                                da = da,
                                di = "00010000",
                                data = data1,
                                time = time
                            });
                            dataList.Add(new C0DData()
                            {
                                da = da,
                                di = "00010100",
                                data = data2,
                                time = time
                            });
                            dataList.Add(new C0DData()
                            {
                                da = da,
                                di = "00010200",
                                data = data3,
                                time = time
                            });
                            dataList.Add(new C0DData()
                            {
                                da = da,
                                di = "00010300",
                                data = data4,
                                time = time
                            });
                            dataList.Add(new C0DData()
                            {
                                da = da,
                                di = "00010400",
                                data = data5,
                                time = time
                            });
                        }
                    }
                    else if(rateNum==0)
                    {
                        for (int i = 0; i < TimeRecordCount; i++)
                        {
                            string data1 = currentDataBlock.Substring(i * currentDataBlockLength * 2 / TimeRecordCount + 2, 8);
                            string time = currentDataBlock.Substring(i * currentDataBlockLength * 2 / TimeRecordCount + 2 + 8, 12);
                            dataList.Add(new C0DData()
                            {
                                da = da,
                                di = "00010000",
                                data = data1,
                                time = time
                            });
                        }
                    }
                    dataRemain = dataRemain.Remove(0, 4 + 8 + currentDataBlockLength * 2);
                }
                #endregion

                #region 0201FF00
                else if (Formater.ReverseHexString(di).Equals("0201FF00"))
                {
                    currentDataBlockLength = (2 * 9 + 6) * TimeRecordCount;
                    currentDataBlock = dataRemain.Substring(12, currentDataBlockLength * 2);
                    for (int i = 0; i < TimeRecordCount; i++)
                    {
                        string data1 = currentDataBlock.Substring(i * currentDataBlockLength * 2 / TimeRecordCount, 4);
                        string data2 = currentDataBlock.Substring(i * currentDataBlockLength * 2 / TimeRecordCount + 4, 4);
                        string data3 = currentDataBlock.Substring(i * currentDataBlockLength * 2 / TimeRecordCount + 4 + 4, 4);
                        string time = currentDataBlock.Substring(i * currentDataBlockLength * 2 / TimeRecordCount + 9 * 4, 12);
                        dataList.Add(new C0DData()
                        {
                            da = da,
                            di = "02010100",
                            data = data1,
                            time = time
                        });
                        dataList.Add(new C0DData()
                        {
                            da = da,
                            di = "02010200",
                            data = data2,
                            time = time
                        });
                        dataList.Add(new C0DData()
                        {
                            da = da,
                            di = "02010300",
                            data = data3,
                            time = time
                        });
                    }
                    dataRemain = dataRemain.Remove(0, 4 + 8 + currentDataBlockLength * 2);
                }
                #endregion

                #region 0202FF00
                else if (Formater.ReverseHexString(di).Equals("0202FF00"))
                {
                    currentDataBlockLength = (3 * 6 + 6) * TimeRecordCount;
                    currentDataBlock = dataRemain.Substring(12, currentDataBlockLength * 2);
                    for (int i = 0; i < TimeRecordCount; i++)
                    {
                        string data1 = currentDataBlock.Substring(i * currentDataBlockLength * 2 / TimeRecordCount, 6);
                        string data2 = currentDataBlock.Substring(i * currentDataBlockLength * 2 / TimeRecordCount + 6, 6);
                        string data3 = currentDataBlock.Substring(i * currentDataBlockLength * 2 / TimeRecordCount + 6 + 6, 6);
                        string time = currentDataBlock.Substring(i * currentDataBlockLength * 2 / TimeRecordCount + 6 * 6, 12);
                        dataList.Add(new C0DData()
                        {
                            da = da,
                            di = "02020100",
                            data = data1,
                            time = time
                        });
                        dataList.Add(new C0DData()
                        {
                            da = da,
                            di = "02020200",
                            data = data2,
                            time = time
                        });
                        dataList.Add(new C0DData()
                        {
                            da = da,
                            di = "02020300",
                            data = data3,
                            time = time
                        });
                    }
                    dataRemain = dataRemain.Remove(0, 4 + 8 + currentDataBlockLength * 2);
                }
                #endregion

                #region 0203FF00
                else if (Formater.ReverseHexString(di).Equals("0203FF00"))
                {
                    currentDataBlockLength = (4 * 3 + 6) * TimeRecordCount;
                    currentDataBlock = dataRemain.Substring(12, currentDataBlockLength * 2);
                    for (int i = 0; i < TimeRecordCount; i++)
                    {
                        string data1 = currentDataBlock.Substring(i * currentDataBlockLength * 2 / TimeRecordCount, 6);
                        string data2 = currentDataBlock.Substring(i * currentDataBlockLength * 2 / TimeRecordCount + 6, 6);
                        string data3 = currentDataBlock.Substring(i * currentDataBlockLength * 2 / TimeRecordCount + 6 + 6, 6);
                        string data4 = currentDataBlock.Substring(i * currentDataBlockLength * 2 / TimeRecordCount + 6 + 6 + 6, 6);
                        string time = currentDataBlock.Substring(i * currentDataBlockLength * 2 / TimeRecordCount + 6 + 6 + 6 + 6, 12);
                        dataList.Add(new C0DData()
                        {
                            da = da,
                            di = "02030000",
                            data = data1,
                            time = time
                        });
                        dataList.Add(new C0DData()
                        {
                            da = da,
                            di = "02030100",
                            data = data2,
                            time = time
                        });
                        dataList.Add(new C0DData()
                        {
                            da = da,
                            di = "02030200",
                            data = data3,
                            time = time
                        });
                        dataList.Add(new C0DData()
                        {
                            da = da,
                            di = "02030300",
                            data = data4,
                            time = time
                        });
                    }
                    dataRemain = dataRemain.Remove(0, 4 + 8 + currentDataBlockLength * 2);
                }
                #endregion

                #region other
                else
                {
                    int dataLength = getDataLength(Formater.ReverseHexString(di));
                    //add 2014-4-16
                    //查询本di长度，为0则不属于日记录，月记录，为数据密度＜5的di，在当前数据中匹配长度
                    if (dataLength == 0)
                    {
                        dataLength = C0CH.GetDataLength(Formater.ReverseHexString(di));
                    }
                    currentDataBlockLength = (dataLength + 6) * TimeRecordCount;
                    currentDataBlock = dataRemain.Substring(12, currentDataBlockLength * 2);
                    for (int i = 0; i < TimeRecordCount; i++)
                    {
                        string data = currentDataBlock.Substring(i * currentDataBlockLength * 2 / TimeRecordCount, dataLength * 2);
                        string time = currentDataBlock.Substring(i * currentDataBlockLength * 2 / TimeRecordCount + dataLength * 2, 12);
                        dataList.Add(new C0DData()
                        {
                            da = da,
                            di = Formater.ReverseHexString(di),
                            data = data,
                            time = time
                        });
                    }
                    
                    dataRemain = dataRemain.Remove(0, 4 + 8 + currentDataBlockLength * 2 );
                }
                #endregion
            }
            return dataList;
        }

        /// <summary>
        /// 解析历史记录
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        //public static List<CDataWithTimeUpline> decodeC0DH(string msg, int TimeRecordCount)
        //{
        //    List<CDataWithTimeUpline> dataList = new List<CDataWithTimeUpline>();

        //    int msgLength = BaseDecoder.GetLength(msg);//字节数

        //    int dataRemainLen = (msgLength - 10) * 2;//初始 剩余未读数据长度
        //    string dataRemain = msg.Substring(32, dataRemainLen);//初始 剩余未读数据



        //    //循环读取块
        //    while (dataRemainLen >= 12)
        //    {
        //        string da = dataRemain.Substring(0, 4);
        //        string di = dataRemain.Substring(4, 8);
        //        int currentDataBlockLength;
        //        string allData;

        //        if (DI0506XXFF.Contains(Formater.ReverseHexString(di)) || DI00XXFF01.Contains(Formater.ReverseHexString(di)) || DI00XXFF00.Contains(Formater.ReverseHexString(di)))
        //        {
        //            //日记录多费率的DI组（4字节的）
        //            int feilvCount = int.Parse(Formater.HEXtoDEC(dataRemain.Substring(12, 2)));
        //            //sb.AppendLine("费率个数:" + feilvCount.ToString() + "\n");
        //            currentDataBlockLength = feilvCount * 4 + 4 + 1;//费率组数(包含总记录4byte)+费率内容
        //            allData = dataRemain.Substring(12, currentDataBlockLength * 2 * TimeRecordCount + TimeRecordCount * 12);
        //            List<string> dataArray = new List<string>();
        //            List<string> timeArray = new List<string>();
        //            for (int i = 0; i < TimeRecordCount; i++)
        //            {
        //                string data = allData.Substring(0, currentDataBlockLength * 2);
        //                data = data.Remove(0, 2);
        //                string dataTem = string.Empty;
        //                //开始解析读取的内容
        //                for (int j = 0; j < feilvCount + 1; j++)
        //                {

        //                    //sb.AppendLine("第" + j.ToString() + "组内容:" + "{" + (data.Substring(0, 8).Length / 2).ToString() + "字节} " + data.Substring(0, 8) + "\n");
        //                    dataTem += data.Substring(0, 8) + " ";
        //                    data = data.Remove(0, 8);
        //                }

        //                string time = allData.Substring(currentDataBlockLength * 2, 12);
        //                allData = allData.Remove(0, currentDataBlockLength * 2 + 12);
        //                dataArray.Add(dataTem); timeArray.Add(time);
        //            }
        //            dataList.Add(new CDataWithTimeUpline { da = da, di = di, dataArray = dataArray, timeArray = timeArray });
        //        }
        //        else
        //        {
        //            currentDataBlockLength = getDataLength(Formater.ReverseHexString(di));
        //            //add 2014-4-16
        //            //查询本di长度，为0则不属于日记录，月记录，为数据密度＜5的di，在当前数据中匹配长度
        //            if (currentDataBlockLength == 0)
        //            {
        //                currentDataBlockLength = C0CH.GetDataLength(Formater.ReverseHexString(di));
        //            }

        //            allData = dataRemain.Substring(12, currentDataBlockLength * 2 * TimeRecordCount + TimeRecordCount * 12);
        //            List<string> dataArray = new List<string>();
        //            List<string> timeArray = new List<string>();
        //            for (int i = 0; i < TimeRecordCount; i++)
        //            {
        //                string data = allData.Substring(0, currentDataBlockLength * 2);
        //                string time = allData.Substring(currentDataBlockLength * 2, 12);
        //                allData = allData.Remove(0, currentDataBlockLength * 2 + 12);
        //                dataArray.Add(data); timeArray.Add(time);
        //            }
        //            dataList.Add(new CDataWithTimeUpline { da = da, di = di, dataArray = dataArray, timeArray = timeArray });
        //        }

        //        //重新计算剩余未读数据
        //        dataRemain = dataRemain.Remove(0, (4 + 8 + currentDataBlockLength * 2 * TimeRecordCount + 12 * TimeRecordCount));
        //        dataRemainLen = dataRemain.Length;
        //    }

        //    return dataList;
        //}
        /// <summary>
        /// 解析历史记录
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="TimeRecordCount"></param>
        /// <returns></returns>
        public static string decodeC0DHToString(string msg, int TimeRecordCount)
        {
            StringBuilder sb = new StringBuilder();

            int msgLength = BaseDecoder.GetLength(msg);//字节数

            int dataRemainLen = (msgLength - 10) * 2;//初始 剩余未读数据长度
            string dataRemain = msg.Substring(32, dataRemainLen);//初始 剩余未读数据

            //循环读取块
            while (dataRemainLen >= 12)
            {
                string da = dataRemain.Substring(0, 4);
                string di = dataRemain.Substring(4, 8);
                int currentDataBlockLength;
                string allData;

                sb.AppendLine("TN:" + BaseDecoder.CalcTn(da) + "\tDI:" + Formater.ReverseHexString(di) + "\n");
                sb.AppendLine("数据内容:\n");


                if (DI0506XXFF.Contains(Formater.ReverseHexString(di)) || DI00XXFF01.Contains(Formater.ReverseHexString(di)) || DI00XXFF00.Contains(Formater.ReverseHexString(di)))
                {
                    //日记录多费率的DI组（4字节的）
                    int feilvCount = int.Parse(Formater.HEXtoDEC(dataRemain.Substring(12, 2)));
                    sb.AppendLine("费率个数:" + feilvCount.ToString() + "\n");
                    currentDataBlockLength = feilvCount * 4 + 4 + 1;//费率组数(包含总记录4byte)+费率内容

                    allData = dataRemain.Substring(12, currentDataBlockLength * 2 * TimeRecordCount + TimeRecordCount * 12);


                    for (int i = 0; i < TimeRecordCount; i++)
                    {
                        string data = allData.Substring(0, currentDataBlockLength * 2);
                        data = data.Remove(0, 2);
                        //开始解析读取的内容
                        for (int j = 0; j < feilvCount + 1; j++)
                        {
                            sb.AppendLine("第" + j.ToString() + "组内容:" + "{" + (data.Substring(0, 8).Length / 2).ToString() + "字节} " + data.Substring(0, 8) + "\n");
                            data = data.Remove(0, 8);
                        }

                        string time = allData.Substring(currentDataBlockLength * 2, 12);
                        sb.AppendLine("时间点:" + time + "\n");
                        allData = allData.Remove(0, currentDataBlockLength * 2 + 12);
                    }

                    //重新计算剩余未读数据
                    dataRemain = dataRemain.Remove(0, (4 + 8 + currentDataBlockLength * 2 * TimeRecordCount + 12 * TimeRecordCount));
                    dataRemainLen = dataRemain.Length;
                }
                else if (Formater.ReverseHexString(di).Equals("E100C010"))
                {
                    currentDataBlockLength = 27;
                    allData = dataRemain.Substring(12, currentDataBlockLength * 2 * TimeRecordCount + TimeRecordCount * 12);
                    for (int i = 0; i < TimeRecordCount; i++)
                    {
                        string data = allData.Substring(0, currentDataBlockLength * 2);
                        sb.AppendLine("数据内容:" + "{" + (data.Length / 2).ToString() + "字节} " + data + "\n");
                        sb.AppendLine("电压监测时间:" + data.Substring(0, 6));
                        sb.AppendLine("电压合格率:" + data.Substring(6, 6));
                        sb.AppendLine("电压超限率:" + data.Substring(12, 6));
                        sb.AppendLine("电压超上限时间:" + data.Substring(18, 6));
                        sb.AppendLine("电压超下限时间:" + data.Substring(24, 6));
                        sb.AppendLine("最高电压:" + data.Substring(30, 4));
                        sb.AppendLine("最高电压出现时间:" + data.Substring(34, 8));
                        sb.AppendLine("最低电压:" + data.Substring(42, 4));
                        sb.AppendLine("最低电压出现时间:" + data.Substring(46, 8));
                        string time = allData.Substring(currentDataBlockLength * 2, 12);
                        sb.AppendLine("时间点:" + time + "\n");
                        allData = allData.Remove(0, currentDataBlockLength * 2 + 12);
                    }
                    //重新计算剩余未读数据
                    dataRemain = dataRemain.Remove(0, (4 + 8 + currentDataBlockLength * 2 * TimeRecordCount + 12 * TimeRecordCount));
                    dataRemainLen = dataRemain.Length;
                }
                else
                {
                    currentDataBlockLength = getDataLength(Formater.ReverseHexString(di));//查询本di长度，为0则不属于日记录，月记录
                    //if (currentDataBlockLength == 0)
                    //{
                    //    break;
                    //}

                    //add 2014-4-16
                    //查询本di长度，为0则不属于日记录，月记录，为数据密度＜5的di，在当前数据中匹配长度
                    if (currentDataBlockLength == 0)
                    {
                        currentDataBlockLength = C0CH.GetDataLength(Formater.ReverseHexString(di));
                    }

                    allData = dataRemain.Substring(12, currentDataBlockLength * 2 * TimeRecordCount + TimeRecordCount * 12);


                    for (int i = 0; i < TimeRecordCount; i++)
                    {
                        string data = allData.Substring(0, currentDataBlockLength * 2);
                        sb.AppendLine("数据内容:" + "{" + (data.Length / 2).ToString() + "字节} " + data + "\n");
                        string time = allData.Substring(currentDataBlockLength * 2, 12);
                        sb.AppendLine("时间点:" + time + "\n");
                        allData = allData.Remove(0, currentDataBlockLength * 2 + 12);
                    }
                    //重新计算剩余未读数据
                    dataRemain = dataRemain.Remove(0, (4 + 8 + currentDataBlockLength * 2 * TimeRecordCount + 12 * TimeRecordCount));
                    dataRemainLen = dataRemain.Length;
                }
                sb.AppendLine("*****************************************************************\n");
            }

            return sb.ToString();
        }

        /// <summary>
        /// 日记录多费率的DI组（4字节的）
        /// </summary>
        public static string[] DI0506XXFF = { "050601FF", "050602FF", "050603FF", "050604FF", 
                                                      "050605FF","050606FF","050607FF", "050608FF" };
        /// <summary>
        /// 月记录多费率的DI组（4字节的）
        /// </summary>
        public static string[] DI00XXFF01 = { "0001FF01", "0002FF01", "0003FF01", "0004FF01", "0005FF01", 
                                                "0006FF01","0007FF01","0008FF01","0009FF01"
                                            };
        /// <summary>
        /// 小时记录多费率DI组（4字节）
        /// </summary>
        public static string[] DI00XXFF00 = { "0000FF00","0001FF00", "0002FF00", "0003FF00", "0004FF00",
                                                "0005FF00","0006FF00","0007FF00"
                                            };
        /// <summary>
        /// 查询 当前数据di的数据域长度，单位字节，当查询结果为0，则说明没有查询到di
        /// </summary>
        /// <param name="diValue">高在前，低在后</param>
        public static int getDataLength(string diValue)
        {
            int length = 0;

            string di3 = diValue.Substring(0, 2);
            string di2 = diValue.Substring(2, 2);
            string di1 = diValue.Substring(4, 2);
            string di0 = diValue.Substring(6, 2);

            #region 日数据，数据密度6
            if (di3.Equals("05") && di2 == "06" && int.Parse(Formater.HEXtoDEC(di1)) <= 8)
            {
                //读取电能值
                return 4;
            }
            if (di3.Equals("05") && di2 == "06" && int.Parse(Formater.HEXtoDEC(di1)) >= 9 && int.Parse(Formater.HEXtoDEC(di1)) <= 12)
            {
                //最大需量
                return 8;
            }
            if (di3.Equals("E1") && di2.Equals("00") && di1.Equals("80") && int.Parse(Formater.HEXtoDEC(di0)) >= 16 && int.Parse(Formater.HEXtoDEC(di0)) <= 19)
            {
                //合格率统计数据
                return 30;
            }
            if (di3.Equals("E1") && di2.Equals("00") && di1.Equals("80") && di0.Equals("14"))
            {
                //
                return 22;
            }
            if (di3.Equals("E1") && di2.Equals("00") && di1.Equals("80") && int.Parse(Formater.HEXtoDEC(di0)) >= 21 && int.Parse(Formater.HEXtoDEC(di0)) <= 25)
            {
                return 14;
            }
            if (di3.Equals("E1") && di2.Equals("00") && di1.Equals("80") && int.Parse(Formater.HEXtoDEC(di0)) >= 26 && int.Parse(Formater.HEXtoDEC(di0)) <= 32)
            {
                return 12;
            }
            if (di3.Equals("E1") && di2.Equals("00") && di1.Equals("80") && int.Parse(Formater.HEXtoDEC(di0)) >= 33 && int.Parse(Formater.HEXtoDEC(di0)) <= 37)
            {
                return 20;
            }
            if (di3.Equals("E1") && di2.Equals("00") && di1.Equals("80") && di0.Equals("26"))
            {
                //
                return 16;
            }
            if (di3.Equals("E1") && di2.Equals("00") && di1.Equals("80") && di0.Equals("27"))
            {
                //
                return 20;
            }
            if (di3.Equals("E1") && di2.Equals("00") && di1.Equals("80") && int.Parse(Formater.HEXtoDEC(di0)) >= 40 && int.Parse(Formater.HEXtoDEC(di0)) <= 42)
            {
                return 24;
            }
            if (di3.Equals("E1") && di2.Equals("00") && di1.Equals("80") && di0.Equals("30"))
            {
                //
                return 3;
            }
            if (di3.Equals("E1") && di2.Equals("00") && di1.Equals("80") && di0.Equals("31"))
            {
                //
                return 3;
            }
            if (di3.Equals("E1") && di2.Equals("00") && di1.Equals("80") && di0.Equals("40"))
            {
                //
                return 72;
            }
            if (di3.Equals("E1") && di2.Equals("00") && di1.Equals("80") && di0.Equals("41"))
            {
                //
                return 40;
            }
            if (di3.Equals("E1") && di2.Equals("00") && di1.Equals("80") && di0.Equals("42"))
            {
                //
                return 40;
            }
            if (di3.Equals("E1") && di2.Equals("00") && int.Parse(Formater.HEXtoDEC(di1)) >= 129 && int.Parse(Formater.HEXtoDEC(di1)) <= 155)
            {
                //
                return 2;
            }
            if (di3.Equals("E1") && di2.Equals("00") && di1.Equals("9C") && int.Parse(Formater.HEXtoDEC(di0)) >= 00 && int.Parse(Formater.HEXtoDEC(di0)) <= 32)
            {
                return 4;
            }
            #endregion

            #region 月数据，，数据密度7
            if (di3.Equals("00") && di0.Equals("01") && int.Parse(Formater.HEXtoDEC(di2)) >= 00 && int.Parse(Formater.HEXtoDEC(di2)) <= 194 &&
                int.Parse(Formater.HEXtoDEC(di1)) >= 0 && int.Parse(Formater.HEXtoDEC(di1)) <= 63)
            {
                return 4;
            }

            if (di3.Equals("01") && int.Parse(Formater.HEXtoDEC(di2)) >= 00 && int.Parse(Formater.HEXtoDEC(di2)) <= 10
                && int.Parse(Formater.HEXtoDEC(di1)) >= 0 && int.Parse(Formater.HEXtoDEC(di1)) <= 63 && di0.Equals("01"))
            {
                return 8;
            }
            if (di3.Equals("01") && int.Parse(Formater.HEXtoDEC(di2)) >= 21 && int.Parse(Formater.HEXtoDEC(di2)) <= 70 &&
                di1.Equals("00") && di0.Equals("01"))
            {
                return 8;
            }
            if (di3.Equals("E1") && di2.Equals("00") && di1.Equals("C0") && int.Parse(Formater.HEXtoDEC(di0)) >= 16 && int.Parse(Formater.HEXtoDEC(di0)) <= 19)
            {
                return 27;
            }
            if (di3.Equals("E1") && di2.Equals("00") && di1.Equals("C0") && di0.Equals("14"))
            {
                return 22;
            }
            if (di3.Equals("E1") && di2.Equals("00") && di1.Equals("C0") && int.Parse(Formater.HEXtoDEC(di0)) >= 21 && int.Parse(Formater.HEXtoDEC(di0)) <= 25)
            {
                return 14;
            }
            if (di3.Equals("E1") && di2.Equals("00") && di1.Equals("C0") && int.Parse(Formater.HEXtoDEC(di0)) >= 26 && int.Parse(Formater.HEXtoDEC(di0)) <= 32)
            {
                return 12;
            }
            if (di3.Equals("E1") && di2.Equals("00") && di1.Equals("C0") && int.Parse(Formater.HEXtoDEC(di0)) >= 33 && int.Parse(Formater.HEXtoDEC(di0)) <= 37)
            {
                return 12;
            }
            if (di3.Equals("E1") && di2.Equals("00") && di1.Equals("C0") && di0.Equals("26"))
            {
                return 16;
            }
            if (di3.Equals("E1") && di2.Equals("00") && di1.Equals("C0") && di0.Equals("27"))
            {
                return 20;
            }
            if (di3.Equals("E1") && di2.Equals("00") && di1.Equals("C0") && di0.Equals("28"))
            {
                return 24;
            }
            if (di3.Equals("E1") && di2.Equals("00") && di1.Equals("C0") && di0.Equals("29"))
            {
                return 24;
            }
            if (di3.Equals("E1") && di2.Equals("00") && di1.Equals("C0") && di0.Equals("2A"))
            {
                return 24;
            }
            if (di3.Equals("E1") && di2.Equals("00") && di1.Equals("C0") && di0.Equals("30"))
            {
                return 3;
            }
            if (di3.Equals("E1") && di2.Equals("00") && di1.Equals("C0") && di0.Equals("31"))
            {
                return 3;
            }
            if (di3.Equals("E1") && di2.Equals("00") && di1.Equals("C0") && di0.Equals("40"))
            {
                return 72;
            }
            if (di3.Equals("E1") && di2.Equals("00") && di1.Equals("C0") && di0.Equals("41"))
            {
                return 40;
            }
            if (di3.Equals("E1") && di2.Equals("00") && di1.Equals("C0") && di0.Equals("42"))
            {
                return 40;
            }
            if (di3.Equals("E1") && di2.Equals("00") && int.Parse(Formater.HEXtoDEC(di1)) >= 193 && int.Parse(Formater.HEXtoDEC(di1)) <= 219 &&
                int.Parse(Formater.HEXtoDEC(di0)) >= 00 && int.Parse(Formater.HEXtoDEC(di0)) <= 21)
            {
                return 2;
            }

            if (di3.Equals("E1") && di2.Equals("00") && di1.Equals("DB") && di0.Equals("20"))
            {
                return 4;
            }
            #endregion

            return length;
        }
    }
}
